#pragma once
#include<iostream>
#include<pthread.h>

class Mutex
{
    private:
        pthread_mutex_t *_lock_p;
        
    public:
        Mutex(pthread_mutex_t *_lock_p_=nullptr) :_lock_p(_lock_p_)  {}

        void lock() {if(_lock_p) pthread_mutex_lock(_lock_p);}

        void unlock() {if(_lock_p) pthread_mutex_unlock(_lock_p);}

        ~Mutex(){}

};


//利用RALL的思想:即将资源交给对象来管理
class LockGuard
{
    private:
        Mutex mutex_;
    
    public:
        LockGuard(pthread_mutex_t *mutex):mutex_(mutex) {mutex_.lock();}

        ~LockGuard(){mutex_.unlock();}

};